package com.lzy.cloud.ticket.train.facade;

import com.lzy.cloud.ticket.train.constant.LMMApi;
import com.lzy.cloud.ticket.train.constant.LMMContant;
import com.lzy.cloud.ticket.train.request.BaseRequest;
import com.lzy.cloud.ticket.train.request.CancelOrderRequest;
import com.lzy.cloud.ticket.train.request.UserInfoRequest;
import com.lzy.cloud.ticket.train.request.app.UserInfoAppRequest;
import com.lzy.cloud.ticket.train.response.BaseResponse;
import com.lzy.cloud.ticket.train.response.UserInfoResponse;
import com.lzy.cloud.ticket.train.util.BasePushUtil;
import com.lzy.cloud.ticket.train.util.RSAUtils;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Component
@Slf4j
public class UserFacade {

    @Resource
    private RestTemplate restTemplate;

    @Autowired
    private BasePushUtil basePushUtil;

    /**
     * 12306身份校验
     * @param userInfoAppRequest
     * @return
     */
    public Result checkAccount(UserInfoAppRequest userInfoAppRequest){
        try {
            String encryptByPub = RSAUtils.encryptByPub(RSAUtils.pubKey, userInfoAppRequest.getUserName());
            String encryptByPub1 = RSAUtils.encryptByPub(RSAUtils.pubKey, userInfoAppRequest.getPassword());
            userInfoAppRequest.setUserName(encryptByPub);
            userInfoAppRequest.setPassword(encryptByPub1);
        }catch (Exception e){
            log.error("00010010");
        }
        BaseRequest baseRequest = basePushUtil.push(LMMContant.APP_KEY,LMMContant.CHECK_ACCOUNT_METHOD,LMMContant.MESSAGE_FORMT,LMMContant.CHECK_ACCOUNT_CALL_BACK_URL);
        UserInfoRequest userInfoRequest = DozerUtils.map(baseRequest,UserInfoRequest.class);
        userInfoRequest.setData(userInfoAppRequest);
        UserInfoResponse userInfoResponse = restTemplate.postForObject(LMMApi.CHECK_ACCOUNT_URL,userInfoRequest,UserInfoResponse.class);
        return new Result(userInfoResponse.getData());
    }

}
